﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace BPILottery
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            AccessConnectionDB();
        }

        //单击抽奖
        private void button1_Click(object sender, EventArgs e)
        {

            button1.Visible = false;
            timer1.Enabled = true;
            StartJudge();


        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        /// <summary>  
        /// Access连接数据库的方法  
        /// </summary>  
        protected void AccessConnectionDB()
        {
            string strConnection = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\yi.zhao\\Desktop\\AccessDB\\BPIUserInfo.accdb";  //C#读取Excel的连接字符串  
            //strConnection += @"Data Source = C:\Users\yi.zhao\Documents\BPIUserInfo.accdb";  //指定数据库在硬盘的物理位置  

            //using (OleDbConnection objConnection = new OleDbConnection(strConnection)) //用using替代objConnection.Close()  
            //{

            OleDbConnection objConnection = new OleDbConnection(strConnection);
            objConnection.Open();  //打开连接   
            OleDbCommand sqlcmd = new OleDbCommand(@"select * from UserInfo", objConnection);  //sql语句 
            OleDbDataReader reader = sqlcmd.ExecuteReader();
            string empNo = "";
            string name = "";

            try
            {
                //using (OleDbDataReader reader = sqlcmd.ExecuteReader())  //执行查询，用using替代reader.Close()  
                //{
                //if (reader.Read())     //这个read调用很重要！不写的话运行时将提示找不到数据  
                while (reader.Read())
                {

                    empNo = reader["人员编号"].ToString();   //取得字段的值  
                    name = reader["姓名"].ToString();   //取得字段的值 

                    //name = reader.GetString(1);
                    //System.Console.WriteLine(reader.GetInt16(0).ToString());
                    //System.Console.WriteLine(reader.GetString(1));
                    //System.Console.WriteLine(reader.GetString(2));
                    //System.Console.WriteLine("\n"); 
                }
                //}
                objConnection.Close();
                reader.Close();
                //}
            }
            catch (OleDbException e)
            {
                Console.WriteLine("error {0}", e.Errors[0].Message);
            }
            this.label1.Text = empNo + "  " + name;
            //this.label2.Text = string.Format(this.label2.Text, name, age);


        }


        private void StartJudge()
        {
            int[] arr = getRandomNum(1,1,20);
            int i = 0;
            string tempRandom = "";
            while (i <= arr.Length - 1)
            {
                tempRandom += arr[i].ToString();
                i++;
            }
            label2.Text = tempRandom;

        }

        //取随机数
        public int[] getRandomNum(int num, int minValue, int maxValue)
        {
            Random random = new Random();
            int[] arrNum = new int[num];
            int tmp = 0;
            for (int i = 0; i <= num - 1; i++)
            {
                //tmp = random.Next(minValue, maxValue);
                tmp = getNum(arrNum, tmp, minValue, maxValue, random);
                arrNum[i] = tmp;
            }

            return arrNum;
        }

        //递归去除重复的值
        public int getNum(int[] arrNum, int tmp, int minValue, int maxValue, Random random)
        {
            int n = 0;
            while (n <= arrNum.Length - 1)
            {
                if (arrNum[n] == tmp)//利用循环判断是否有重复
                {
                    tmp = random.Next(minValue, maxValue);
                }
                n++;
            }
            return tmp;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }



    }
}
